package org.JWCB.system.manager;

import org.JWCB.common.core.constants.CacheConstants;
import org.JWCB.common.redis.service.RedisService;
import org.JWCB.system.domain.exam.entity.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExamCacheManager {
    @Autowired
    private RedisService redisService;

    /**
     * 将发布的竞赛缓存到redis
     * @param exam 竞赛信息
     */
    public void addCache(Exam exam) {
        redisService.leftPushForList(getExamListKey(),exam.getExamId());
        redisService.setCacheObject(getDetailKey(exam.getExamId()),exam);
    }


    /**
     * 取消发布竞赛从缓存中删除
     */

    public void delCache(Exam exam) {
        redisService.removeForList(getExamListKey(),exam.getExamId());
        redisService.deleteObject(getDetailKey(exam.getExamId()));
    }


    /**
     * 删除竞赛题目题目缓存
     */
    public void delExamQuestionListCache(Long examId) {
        redisService.deleteObject(getExamQuestionListKey(examId));
    }
    /**
     * 得到未完赛的list缓存key
     * @return key
     */
    private String getExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST_KEY;
    }

    /**
     * 得到竞赛详情的换成key
     * @return key
     */
    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL_KEY + examId;
    }



    private String getExamQuestionListKey(Long examId) {
        return CacheConstants.EXAM_QUESTION_LIST_KEY +examId;
    }
}
